package model;

/**
 * This class holds two ints (row and col) that represent locations
 * in a grid. (0,0) is the far upper left corner of any GridGame board.
 * @author Sacha Best
 */
public class Location
{
	/**
	 * These constant represent all of the compass directions.
	 */
	public static final int NORTH = 1, NORTHEAST = 2, EAST = 3, SOUTHEAST = 4, SOUTH = 5, SOUTHWEST = 6, WEST = 7, NORTHWEST = 8;
	/**
	 * The ints representing the row and column of the 
	 * Location object.
	 */
	private int row, col;
	
	/**
	 * Creates a new Location object.
	 * @param row - the row.
	 * @param col - the column.
	 */
	public Location(int row, int col)
	{
		this.row = row;
		this.col = col;
	}
	
	/**
	 * @return the row.
	 */
	public int getRow()
	{
		return row;
	}
	/**
	 * @return the column.
	 */
	public int getCol()
	{
		return col;
	}
	/**
	 * This method returns the {@link model.Location} adjacent to this in a given direction defined by the constants 
	 * in this class.
	 * @param direction - the direction in which the adjacent {@link model.Location} lies
	 * @return the {@link model.Location} adjacent to this in the given direction
	 */
	public Location getAdjacentLocation(int direction)
	{
		Location tempLoc = null;
		switch (direction)
		{
		case Location.NORTH:
			tempLoc = new Location(this.getRow() - 1, this.getCol());
			break;
		case Location.NORTHEAST:
			tempLoc = new Location(this.getRow() - 1, this.getCol() + 1);
			break;
		case Location.EAST:
			tempLoc = new Location(this.getRow(), this.getCol() + 1);
			break;
		case Location.SOUTHEAST:
			tempLoc = new Location(this.getRow() + 1, this.getCol() + 1);
			break;
		case Location.SOUTH:
			tempLoc = new Location(this.getRow() + 1, this.getCol());
			break;
		case Location.SOUTHWEST:
			tempLoc = new Location(this.getRow() + 1, this.getCol() - 1);
			break;
		case Location.WEST:
			tempLoc = new Location(this.getRow(), this.getCol() - 1);
			break;
		case Location.NORTHWEST:
			tempLoc = new Location(this.getRow() - 1, this.getCol() - 1);
			break;
		}
		return tempLoc;
	}
	/**
	 * Determines whether or not two Location objects are equal by comparing
	 * their row and column values
	 * @param other - the other Location
	 */
	@Override
	public boolean equals(Object other)
	{
		if (!(other instanceof Location))
			return false;
		else
		{
			Location compare = (Location) other;
			return getRow() == compare.getRow() && getCol() == compare.getCol();
		}
	}
	/**
	 * This method returns a String representation of the {@link model.Location} object
	 * @return "Row: x Col: y"
	 */
	@Override
	public String toString()
	{
		return "Row: " + row + " Col: " + col;
	}
	/**
	 * This method turns a Location object into a string. It is static because it does not act upon
	 * and specific {@link model.Location} object, but rasther a String
	 * @param input - the String to be parsed
	 * @return a Location baed upon input, null if input is null
	 */
	public static Location stringToLoc(String input)
	{
		if (input == null)
			return null;
		int row = Integer.parseInt(input.substring(5,input.indexOf('C')-1));
		int col = Integer.parseInt(input.substring(input.indexOf('C')+5));
		return new Location(row,col);
	}
}
